﻿using CMS.Models;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class InputCargoViewModel : ViewModelBase
    {



        /// <summary>
        /// 当前物资
        /// </summary>
        public Cargo Cargo { get; set; }

        public AppData AppData { get; set; } = AppData.Instance;

        private Record records = new Record();
        /// <summary>
        /// 出库流水单
        /// </summary>
        public Record Records
        {
            get { return records; }
            set { records = value; RaisePropertyChanged(); }

        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        public RelayCommand<Window> CloseCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    if (arg == null) return;
                    arg.Close();
                });
            }
        }





        /// <summary>
        /// 物品入库
        /// </summary>
        public RelayCommand<Window> AddRecordCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    // if (string.IsNullOrEmpty(records.CargoName)) return;
                    // var cargo = this.Cargos.FirstOrDefault(item => item.Name == records.CargoName);
                    if (Cargo == null) return;
                    records.CargoId = Cargo.Id;
                    records.CargoName = Cargo.Name;
                    if (records.Number == 0) return;
                    records.InsertDate = DateTime.Now;
                    records.MemberId = AppData.CurrentUser.Id;
                    records.MemberName = AppData.CurrentUser.Name;
                    records.RecordType = true; // 表示入库

                    var count = new RecordProvider().Insert(records);
                    if (count == 0)
                    {
                        MessageBox.Show("添加失败");
                    }
                    else
                    {
                        arg.Close();
                    }
                });
            }
        }
    }
}
